import 'package:boss/models/coupon.dart';

import 'package:boss/ui/page/marketing/coupon/couponDetail/provider/coupon_detail_provider.dart';
import 'package:boss/ui/widget/alert/alert.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

import 'widget/coupon_detail_browser.dart';

@RouterConfig(CouponDetailPage.pageName)
class CouponDetailPage extends StatefulWidget {
  static const String pageName = '/CouponDetailPage';

  @Autowired()
  final Coupon coupon;

  const CouponDetailPage({this.coupon});

  @override
  _CouponDetailPageState createState() => _CouponDetailPageState();
}

class _CouponDetailPageState extends BaseState<CouponDetailPage> {
  _CouponDetailPageState() : super(CouponDetailPage.pageName);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (ctx) => CouponDetailProvider(widget.coupon),
      child: Builder(
        builder: (ctx) => Scaffold(
          appBar: CenterTitleAppBar(
            title: '${widget.coupon == null ? '新增' : '编辑'}优惠券',
            actions: <Widget>[
              Consumer<CouponDetailProvider>(
                builder: (context, provider, _) {
                  return provider.originCoupon == null
                      ? SizedBox()
                      : LabelButton(
                          padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
                          title: provider.coupon.usingStatus == Coupon.STATUS_OFF ? '已结束' : '结束',
                          onTap: provider.coupon.usingStatus == Coupon.STATUS_OFF
                              ? null
                              : () {
                                  showAlert(context, content: '确认提前结束优惠券吗？', onCancel: (context) {
                                    Navigator.pop(context);
                                  }, onConfirm: (context) {
                                    Provider.of<CouponDetailProvider>(ctx, listen: false).closeCoupon();
                                    Navigator.pop(context);
                                  });
                                });
                },
              ),
            ],
          ),
          body: CouponDetailBrowser(),
        ),
      ),
    );
  }
}
